import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import AdminList from './components/user/admin/AdminList.vue'
import StudentList from './components/user/student/StudentList.vue'
import Chapter from './components/chapter/Chapter.vue'
import VideoList from './components/video/VideoList.vue'
import VideoEdit from './components/video/VideoEdit.vue'
import OfficeList from './components/office/OfficeList.vue'
import OfficeEdit from './components/office/OfficeEdit.vue'
import LearningRecordes from './components/learningRecordes/index.vue';
import Question from './components/question/Question.vue'
import SingleChoice from './components/question/components/SingleChoice.vue'
import MultipleChoice from './components/question/components/MultipleChoice.vue'
import TrueFalse from './components/question/components/TrueFalse.vue'
import Exam from './components/exam/Exam.vue'
import ExamEdit from './components/exam/Edit.vue'
import ExamAnswerList from './components/examAnswer/list.vue'
import ExamAnswerRead from './components/examAnswer/read.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/home/welcome',
    children: [
      { path: 'welcome', component: Welcome },
      { path: '/user/adminList', component: AdminList },
      { path: '/user/studentList', component: StudentList },
      { path: '/chapter', component: Chapter},
      { path: '/video/list', component: VideoList},
      { path: '/video/edit', component: VideoEdit},
      { path: '/office/list', component: OfficeList},
      { path: '/office/edit', component: OfficeEdit},
      { path: '/learningRecordes/list', component: LearningRecordes},
      { path: '/question/list', component: Question },
      { path: '/question/single-choice', component: SingleChoice },
      { path: '/question/multiple-choice', component: MultipleChoice },
      { path: '/question/true-false', component: TrueFalse },
      { path: '/exam/list', component: Exam },
      { path: '/exam/edit', component: ExamEdit },
      { path: '/examAnswer/list', component: ExamAnswerList },
      { path: '/examAnswer/read', component: ExamAnswerRead },
    ],
  },
]

const router = new VueRouter({
  routes,
})

// 挂载路由导航守卫
// 此处没有判断token的值是否正确，要想判断就得去后端验证，所以就有一个单点登录系统的出现
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行 next('/login') 强制跳转

  if (to.path === '/login') {
    // return next()
  } else {
    // token name 配置出去
    const tokenStr = window.sessionStorage.getItem(
      Vue.prototype.globalVariable.tokenName
    )
    if (!tokenStr) {
      return next('/login')
    }
  }
  
  next()
})

export default router
